﻿namespace Entities
{
  using System;
  using System.Web;

  public class CartItem
  {
    public CartItem()
    {
    }

    public CartItem(String cartId)
    {
      this.CartId = cartId;
    }

    public Int32 Id { get; set; }

    public String CartId { get; set; }

    public Int32 DocumentId { get; set; }

    public virtual Document Document { get; set; }

    public static String GetCartId(HttpContextBase context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      if (context.Session == null)
      {
        // This should not be possible, however put in to remove resharper warning
        throw new ArgumentException("context.Session is null");
      }

      if (context.Session["CartId"] != null)
      {
        return context.Session["CartId"].ToString();
      }
      if (!String.IsNullOrWhiteSpace(context.User.Identity.Name))
      {
        context.Session["CartId"] = context.User.Identity.Name;
      }
      else
      {
        Guid tempCartId = Guid.NewGuid();
        context.Session["CartId"] = tempCartId.ToString();
      }

      return context.Session["CartId"].ToString();
    }
  }
}
